#include <bits/stdc++.h>
#define ll long long
#define str string
#define N 2003
using namespace std;

int n;
int a[N];

ll f[2][N][N];

void solve(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    memset(f,0,sizeof(f));
    for(int i=n+1;i>0;i--){
        for(int j=0;j<n;j++){
            for(int k=0;k<n;k++){
                f[i&1][j][k]=max(f[(i&1)^1][i][k]+1LL*(a[i]==a[j])*a[i],
                        f[(i&1)^1][j][i]+1LL*(a[i]==a[k])*a[i]);
            }
        }
    }
    printf("%lld\n",f[1][0][0]);
}

int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--) solve();
    return 0;
}

